package com.d3works.apps.sale.web;

import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.repo.SaleOrderRepository;
import com.d3works.apps.sale.service.saleorder.SaleOrderNotifyService;
import com.d3works.exception.ResponseMessageType;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;
import java.lang.invoke.MethodHandles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class SaleOrderNotifyController {
  private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  public void confirmSaleOrderNotify(ActionRequest request, ActionResponse response) {
    try {
      SaleOrder saleOrder = request.getContext().asType(SaleOrder.class);

      Beans.get(SaleOrderNotifyService.class)
          .onOrderCreate(Beans.get(SaleOrderRepository.class).find(saleOrder.getId()));

      response.setReload(false);
    } catch (Exception e) {
      TraceBackService.trace(response, e, ResponseMessageType.ERROR);
    }
  }
}
